package com.twu.biblioteca;

import java.lang.reflect.Field;
import java.util.ArrayList;

/**
 * Created by Nishi on 8/1/14.
 * Library of books and movies
 */

public class Library<E> {

    public String displayItemDetails(ArrayList<E> itemList) {
        String allBookDetails = "";
        for ( int index = 0 ;index < itemList.size();index++){
            allBookDetails += itemList.get(index).toString();
        }
        return allBookDetails;
    }

    public boolean isValidItemName(String name, ArrayList<E> itemList) {
        String category = getItemCategory(itemList);

        for (E item: itemList )
        {
            if ((category == "BOOK" && ((Book)item).getBookName().toUpperCase().equals((name.toUpperCase())) ) ||
                    (category == "MOVIE" && ((Movie)item).getMovieName().toUpperCase().equals((name.toUpperCase())))){
                return true;
            }
        }
        return false;
    }
    public  boolean isItemQuantityAvailable(String name, ArrayList<E> itemList) throws NoSuchFieldException, IllegalAccessException {
        if ((itemQuantityAvailable(name, itemList)>0))
            return true;
        else
            return false;
    }

    public  int itemQuantityAvailable(String name, ArrayList<E> itemList) throws NoSuchFieldException, IllegalAccessException {
          for (E item : itemList) {
            if (((item.getClass()== Book.class)&&((Book)item).getBookName().toUpperCase().equals((name.toUpperCase()))) ||
                    ((item.getClass()== Movie.class)&&((Movie)item).getMovieName().toUpperCase().equals((name.toUpperCase())))){
                Field field = item.getClass().getDeclaredField("availableQuantity");
                field.setAccessible(true);
                field.getInt(item);
                return field.getInt(item);
          }
        }
        return 0;
    }

    public  E getItem(String name, ArrayList<E> itemList){
        for (E item: itemList){
            if (((item.getClass()== Book.class)&&((Book)item).getBookName().toUpperCase().equals((name.toUpperCase()))) ||
                    ((item.getClass()== Movie.class)&&((Movie)item).getMovieName().toUpperCase().equals((name.toUpperCase())))){
                return item;
            }
        }
        return null;
    }

    public  boolean isSuccessfulCheckOut(String name, ArrayList<E> itemList) throws NoSuchFieldException,
                                                                                IllegalAccessException {
        Library library = new Library();
        if (library.isValidItemName(name,itemList) && library.isItemQuantityAvailable(name, itemList)){
            return true;
        }
        else
            return false;
    }

    public  void updateItemDetailsOnCheckOut(String name, ArrayList<E> itemList, Customer loggedCustomer) throws NoSuchFieldException, IllegalAccessException {
        Library library = new Library();
        Book checkOutBook;
        Movie checkOutMovie;
        if ( library.isSuccessfulCheckOut(name, itemList) ){
            if (library.getItem(name, itemList).getClass()== Book.class){
                checkOutBook= (Book) library.getItem(name, itemList);
                checkOutBook.setAvailableQuantity((checkOutBook.getAvailableQuantity())-1);
                loggedCustomer.addBooks(checkOutBook.getBookName());
            }
            else {
                checkOutMovie = (Movie)library.getItem(name, itemList);
                checkOutMovie.setAvailableQuantity((checkOutMovie.getAvailableQuantity())-1);
            }
        }
    }

    public  void updateItemDetailsOnReturn(String name, ArrayList<E> itemList, Customer loggedCustomer) {
        Library library = new Library();
        Book checkInBook;
        Movie checkInMovie;
        if(isSuccessfulReturn(name,loggedCustomer)){
            if (itemList.get(0).getClass()== Book.class){
                checkInBook=(Book) library.getItem(name, itemList);
                checkInBook.setAvailableQuantity((checkInBook.getAvailableQuantity())+1);
                loggedCustomer.checkedOutBooks.remove(checkInBook.getBookName());
            }
            else    // functionality not fully implemented for return movie
            {
                checkInMovie=(Movie) library.getItem(name, itemList);
                checkInMovie.setAvailableQuantity((checkInMovie.getAvailableQuantity())+1);
            }
        }
    }
    public  String checkOutItemStatusMessage(boolean status,String itemCategory){
        if(status)
            return ("Thank you! Enjoy the "+itemCategory);
        else
            return ("That "+itemCategory+" is NOT Available");
    }

    public static String returnItemStatusMessage(boolean validBookName,String itemCategory) {
        if(validBookName)
            return ("Thank you for returning the "+ itemCategory);
        else
            return ("That is not a valid "+itemCategory+" to return");
    }

  public  void checkOutItem( String name,ArrayList<E> itemList, Customer loggedCustomer) throws NoSuchFieldException, IllegalAccessException {
      Library library = new Library();
      String itemCategory = getItemCategory(itemList);
      String checkOutMessage = library.checkOutItemStatusMessage(library.isSuccessfulCheckOut(name, itemList), itemCategory);
      System.out.println(checkOutMessage);
      library.updateItemDetailsOnCheckOut(name, itemList, loggedCustomer);
  }

    public  void returnItem( String name,ArrayList<E> itemList,Customer loggedCustomer) throws
            NoSuchFieldException, IllegalAccessException {
        Library library = new Library();
        String itemCategory = getItemCategory(itemList);
        String returnMessage = library.returnItemStatusMessage(library.isSuccessfulReturn(name,loggedCustomer),
                               itemCategory);
        System.out.println(returnMessage);
        library.updateItemDetailsOnReturn(name, itemList,loggedCustomer);
    }
    private String getItemCategory(ArrayList<E> itemList) {
        String itemCategory="MOVIE";
        if(itemList.get(0).getClass() == Book.class){
            itemCategory = "BOOK";
        }
        return itemCategory;
    }

    public boolean isSuccessfulReturn(String name,Customer loggedUser){
        for(String bookName: loggedUser.checkedOutBooks){
            if(bookName.toUpperCase().equals(name.toUpperCase())){
                return true;
            }
        }
        return  false;
    }
  }
